package com.louzhu.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.louzhu.common.utils.Query;
import com.louzhu.gulimall.product.entity.AttrEntity;
import com.louzhu.gulimall.product.service.AttrService;
import com.louzhu.gulimall.product.vo.AttrGroupWithAttrVo;
import com.louzhu.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.louzhu.common.utils.PageUtils;


import com.louzhu.gulimall.product.dao.AttrGroupDao;
import com.louzhu.gulimall.product.entity.AttrGroupEntity;
import com.louzhu.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;
    /**
     * 根据分类获取属性分组
     * @param params 查询条件
     * @param catelogId 分类ID
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catelogId) {

        // 获取查询关键词
        String key = (String)params.get("key");
        // 查询条件器
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();

        // 如果查询条件：关键词不为空字符串，将其加入查询条件：id等于关键词或者name包含关键词
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->
                obj.eq("attr_group_id",key).or().like("attr_group_name",key)
            );
        }

        // 如果请求路径中的参数：分类ID为0，表示查询所有分类关联的属性分组,请求参数分类ID不为0，查询条件器中加入条件
        if(catelogId != 0){
            wrapper.eq("catelog_id",catelogId);
        }

        // 自定义的Query工具类把map封装为IPage
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
        return new PageUtils(page);
    }

    /**
     * 根据三级分类ID查出所有的属性分组及其下面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrVo> getAttrGroupWithAttrByCatelogId(Long catelogId) {
        // 查出三级分类下的所有分组
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        // 查询每个分组下的属性
        List<AttrGroupWithAttrVo> attrGroupWithAttrVos = attrGroupEntities.stream().map(item -> {
            // 封装数据的vo对象
            AttrGroupWithAttrVo attrGroupWithAttrVo = new AttrGroupWithAttrVo();
            // 属性对拷
            BeanUtils.copyProperties(item,attrGroupWithAttrVo);
            // 根据分组ID查询关联的属性
            if(null != attrGroupWithAttrVo.getAttrGroupId()){
                List<AttrEntity> relationAttrs = attrService.getRelationAttr(attrGroupWithAttrVo.getAttrGroupId());
                // 查询为null 是置为空集合，避免前端使用for each循环时出现null错误
                if(null == relationAttrs){
                    relationAttrs = new ArrayList<>();
                }
                attrGroupWithAttrVo.setAttrs(relationAttrs);
            }
            return attrGroupWithAttrVo;
        }).collect(Collectors.toList());
        return attrGroupWithAttrVos;
    }

    /**
     * 查出当前spu所有属性对应的属性分组和属性值
     * @param spuId
     * @param catelogId
     * @return
     */
    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catelogId) {
        AttrGroupDao attrGroupDao = this.baseMapper;
        List<SpuItemAttrGroupVo> resut = attrGroupDao.getAttrGroupWithAttrsBySpuId(spuId,catelogId);
        return resut;
    }

}